package ru.hh.school.util;

import java.util.LinkedList;

public class BlockingQueue<T> {

    private final LinkedList<T> queue;
    private int max_number;

    public BlockingQueue(int max_number) {
        this.queue = new LinkedList<T>();
        this.max_number = max_number;
    }

    public void add(T obj) {
        synchronized (queue) {
            if (queue.size() < this.max_number) {
                queue.add(obj);
            }
            queue.notify();
        }
    }

    public T take() throws InterruptedException {
        synchronized (queue) {
            while (queue.isEmpty()) {
                queue.wait();
            }
            return queue.removeFirst();
        }
    }

}
